﻿/*
 *page插件，拼接url参数，重新发起查询请求
 */
; (function ($) {
    $.fn.Page = function (options) {
        var defaults = {
            linkClass: "page-link",
            query: [],//[["url参数名","控件Id"],]
            bt: null,//查询按钮的Id
            changes: [],//[] 多选框change事件
        };

        var _options = $.extend(defaults, options);
        function getUrlModel() {
            var url = new URL(document.location.href);
            _options.query.forEach(function (m, i, arr) {
                console.log(m)
                var val = $("#" + m[1]).val();
                console.log(val);
                if (val !== null && val !== undefined && val !== "") {
                    url.searchParams.set(m[0], val);
                }
            })
            return url;
        };
        function onClick() {
            var url = getUrlModel();
            //console.log(url.href);
            console.log($(this));
            if ($(this).data("pg-i") !== undefined) {
                url.searchParams.set($(this).data("pg-n"), $(this).data("pg-i"));
            }
            console.log(url.href);
            location.href = url.href;
        }
        return this.each(function () {
            //分页按钮绑定事件
            $("." + _options.linkClass).each(function (i, e) {
                $(e).click(onClick);
            })
            //查询按钮
            if (_options.bt !== null) {
                //console.log($("#" + _options.bt))
                $("#" + _options.bt).click(onClick);
            }
            //自定义的事件绑定
            if (_options.changes.length > 0) {
                _options.changes.forEach(function (m) {
                    $("#" + m).on("change", onClick)
                })
            }
        });
    };
})(jQuery);